import configparser
import redis.asyncio as redis
from redis.asyncio.retry import Retry
from redis.backoff import ExponentialBackoff
from redis.asyncio import ConnectionPool

# 创建Configparser对象
config = configparser.ConfigParser()
# 读取config.ini文件配置
config.read('config.ini')
redis_host = config.get('Redis', 'HOST')
redis_port = config.get('Redis', 'PORT')
redis_pwd = config.get('Redis', 'PASSWORD')

# redis配置
# 设置失败间隔
backoff = ExponentialBackoff(cap=2, base=2)
# 设置重试策略
retry = Retry(backoff=backoff, retries=10)
# 创建连接池
redis_pool = ConnectionPool(
    host=redis_host,
    port=redis_port,
    password=redis_pwd,
    decode_responses=True, # 解码 Redis 返回的字符串为 UTF-8
    retry=retry, # 设置重试策略
    socket_timeout=60, # 读取超时
    socket_connect_timeout=60, # 连接超时
    socket_keepalive=True, # 保持活动连接
    health_check_interval=60, # 每 60 秒检查一次连接的健康状态
    max_connections=10 # 最大连接数
)
# 创建客户端
redis_client = redis.Redis(connection_pool=redis_pool)